package com.item;

import edu.emory.mathcs.backport.java.util.Arrays;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public class ForCodeTest {

    private  List<String> list = new ArrayList<>();
    {
        for(int i =0;i<1000000;i++){
            list.add(String.valueOf(i));
        }
    }

    @Benchmark
    public void forFunction() throws InterruptedException {
        for(int i = 0;i<list.size();i++){
            list.get(i);
        }
//        Thread.sleep(10);
    }

    @Benchmark
    public void ArrayListSteam() throws InterruptedException {
        list.forEach(item->item.toString());
//        Thread.sleep(10);
    }

    @Benchmark
    public void ArrayListParallelStream() throws InterruptedException {
        list.parallelStream().forEach(item->item.toString());
//        Thread.sleep(10);
    }


    public void foreachFunction(){
        int[] num = new int[1000000];
        IntStream.range(0,1000000).forEach(item -> num[item] = item);
    }

    public void forStreamFunction(){
        int[] num =  IntStream.range(0,1000000).toArray();
    }

    public static void main(String[] args) throws Exception{
        Options opt = new OptionsBuilder()
                .include(ForCodeTest.class.getSimpleName())
                .build();
        new Runner(opt).run();
    }


}
